home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / layout / nsStyleCoord.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  9KB  |  293 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsStyleCoord_h___
  38. #define nsStyleCoord_h___
  39.  
  40. #include "nscore.h"
  41. #include "nsCoord.h"
  42. #include "nsCRT.h"
  43. #include "nsStyleConsts.h"
  44. class nsString;
  45.  
  46. enum nsStyleUnit {
  47.   eStyleUnit_Null         = 0,      // (no value) value is not specified
  48.   eStyleUnit_Normal       = 1,      // (no value)
  49.   eStyleUnit_Auto         = 2,      // (no value)
  50.   eStyleUnit_Percent      = 10,     // (float) 1.0 == 100%
  51.   eStyleUnit_Factor       = 11,     // (float) a multiplier
  52.   eStyleUnit_Coord        = 20,     // (nscoord) value is twips
  53.   eStyleUnit_Integer      = 30,     // (int) value is simple integer
  54.   eStyleUnit_Proportional = 31,     // (int) value has proportional meaning
  55.   eStyleUnit_Enumerated   = 32,     // (int) value has enumerated meaning
  56.   eStyleUnit_Chars        = 33      // (int) value is number of characters
  57. };
  58.  
  59. typedef union {
  60.   PRInt32     mInt;   // nscoord is a PRInt32 for now
  61.   float       mFloat;
  62. } nsStyleUnion;
  63.  
  64. /**
  65.  * Class that hold a single size specification used by the style
  66.  * system.  The size specification consists of two parts -- a number
  67.  * and a unit.  The number is an integer, a floating point value, an
  68.  * nscoord, or undefined, and the unit is an nsStyleUnit.  Checking
  69.  * the unit is a must before asking for the value in any particular
  70.  * form.
  71.  */
  72. class nsStyleCoord {
  73. public:
  74.   nsStyleCoord(nsStyleUnit aUnit = eStyleUnit_Null);
  75.   nsStyleCoord(nscoord aValue);
  76.   nsStyleCoord(PRInt32 aValue, nsStyleUnit aUnit);
  77.   nsStyleCoord(float aValue, nsStyleUnit aUnit);
  78.   nsStyleCoord(const nsStyleCoord& aCopy);
  79.  
  80.   nsStyleCoord&  operator=(const nsStyleCoord& aCopy);
  81.   PRBool         operator==(const nsStyleCoord& aOther) const;
  82.   PRBool         operator!=(const nsStyleCoord& aOther) const;
  83.  
  84.   nsStyleUnit GetUnit(void) const { return mUnit; }
  85.   nscoord     GetCoordValue(void) const;
  86.   PRInt32     GetIntValue(void) const;
  87.   float       GetPercentValue(void) const;
  88.   float       GetFactorValue(void) const;
  89.   void        GetUnionValue(nsStyleUnion& aValue) const;
  90.  
  91.   void  Reset(void);  // sets to null
  92.   void  SetCoordValue(nscoord aValue);
  93.   void  SetIntValue(PRInt32 aValue, nsStyleUnit aUnit);
  94.   void  SetPercentValue(float aValue);
  95.   void  SetFactorValue(float aValue);
  96.   void  SetNormalValue(void);
  97.   void  SetAutoValue(void);
  98.   void  SetUnionValue(const nsStyleUnion& aValue, nsStyleUnit aUnit);
  99.  
  100.   void  AppendToString(nsString& aBuffer) const;
  101.   void  ToString(nsString& aBuffer) const;
  102.  
  103. public:
  104.   nsStyleUnit   mUnit;
  105.   nsStyleUnion  mValue;
  106. };
  107.  
  108.  
  109. /**
  110.  * Class that represents a set of top/right/bottom/left nsStyleCoords.
  111.  * This is commonly used to hold the widths of the borders, margins,
  112.  * or paddings of a box.
  113.  */
  114. class nsStyleSides {
  115. public:
  116.   nsStyleSides(void);
  117.  
  118. //  nsStyleSides&  operator=(const nsStyleSides& aCopy);  // use compiler's version
  119.   PRBool         operator==(const nsStyleSides& aOther) const;
  120.   PRBool         operator!=(const nsStyleSides& aOther) const;
  121.  
  122.   // aSide is always one of NS_SIDE_* defined in nsStyleConsts.h
  123.  
  124.   inline nsStyleUnit GetUnit(PRUint8 aSide) const;
  125.   inline nsStyleUnit GetLeftUnit(void) const;
  126.   inline nsStyleUnit GetTopUnit(void) const;
  127.   inline nsStyleUnit GetRightUnit(void) const;
  128.   inline nsStyleUnit GetBottomUnit(void) const;
  129.  
  130.   inline nsStyleCoord& Get(PRUint8 aSide, nsStyleCoord& aCoord) const;
  131.   inline nsStyleCoord& GetLeft(nsStyleCoord& aCoord) const;
  132.   inline nsStyleCoord& GetTop(nsStyleCoord& aCoord) const;
  133.   inline nsStyleCoord& GetRight(nsStyleCoord& aCoord) const;
  134.   inline nsStyleCoord& GetBottom(nsStyleCoord& aCoord) const;
  135.  
  136.   void  Reset(void);
  137.  
  138.   inline void Set(PRUint8 aSide, const nsStyleCoord& aCoord);
  139.   inline void SetLeft(const nsStyleCoord& aCoord);
  140.   inline void SetTop(const nsStyleCoord& aCoord);
  141.   inline void SetRight(const nsStyleCoord& aCoord);
  142.   inline void SetBottom(const nsStyleCoord& aCoord);
  143.  
  144.   void  AppendToString(nsString& aBuffer) const;
  145.   void  ToString(nsString& aBuffer) const;
  146.  
  147. protected:
  148.   PRUint8       mUnits[4];
  149.   nsStyleUnion  mValues[4];
  150. };
  151.  
  152. // -------------------------
  153. // nsStyleCoord inlines
  154. //
  155. inline PRBool nsStyleCoord::operator!=(const nsStyleCoord& aOther) const
  156. {
  157.   return PRBool(! ((*this) == aOther));
  158. }
  159.  
  160. inline PRInt32 nsStyleCoord::GetCoordValue(void) const
  161. {
  162.   NS_ASSERTION((mUnit == eStyleUnit_Coord), "not a coord value");
  163.   if (mUnit == eStyleUnit_Coord) {
  164.     return mValue.mInt;
  165.   }
  166.   return 0;
  167. }
  168.  
  169. inline PRInt32 nsStyleCoord::GetIntValue(void) const
  170. {
  171.   NS_ASSERTION((mUnit == eStyleUnit_Proportional) ||
  172.                (mUnit == eStyleUnit_Enumerated) ||
  173.                (mUnit == eStyleUnit_Chars) ||
  174.                (mUnit == eStyleUnit_Integer), "not an int value");
  175.   if ((mUnit == eStyleUnit_Proportional) ||
  176.       (mUnit == eStyleUnit_Enumerated) ||
  177.       (mUnit == eStyleUnit_Chars) ||
  178.       (mUnit == eStyleUnit_Integer)) {
  179.     return mValue.mInt;
  180.   }
  181.   return 0;
  182. }
  183.  
  184. inline float nsStyleCoord::GetPercentValue(void) const
  185. {
  186.   NS_ASSERTION(mUnit == eStyleUnit_Percent, "not a percent value");
  187.   if (mUnit == eStyleUnit_Percent) {
  188.     return mValue.mFloat;
  189.   }
  190.   return 0.0f;
  191. }
  192.  
  193. inline float nsStyleCoord::GetFactorValue(void) const
  194. {
  195.   NS_ASSERTION(mUnit == eStyleUnit_Factor, "not a factor value");
  196.   if (mUnit == eStyleUnit_Factor) {
  197.     return mValue.mFloat;
  198.   }
  199.   return 0.0f;
  200. }
  201.  
  202. inline void nsStyleCoord::GetUnionValue(nsStyleUnion& aValue) const
  203. {
  204.   memcpy(&aValue, &mValue, sizeof(nsStyleUnion));
  205. }
  206.  
  207. // -------------------------
  208. // nsStyleSides inlines
  209. //
  210. inline PRBool nsStyleSides::operator!=(const nsStyleSides& aOther) const
  211. {
  212.   return PRBool(! ((*this) == aOther));
  213. }
  214.  
  215. inline nsStyleUnit nsStyleSides::GetUnit(PRUint8 aSide) const
  216. {
  217.   return (nsStyleUnit)mUnits[aSide];
  218. }
  219.  
  220. inline nsStyleUnit nsStyleSides::GetLeftUnit(void) const
  221. {
  222.   return GetUnit(NS_SIDE_LEFT);
  223. }
  224.  
  225. inline nsStyleUnit nsStyleSides::GetTopUnit(void) const
  226. {
  227.   return GetUnit(NS_SIDE_TOP);
  228. }
  229.  
  230. inline nsStyleUnit nsStyleSides::GetRightUnit(void) const
  231. {
  232.   return GetUnit(NS_SIDE_RIGHT);
  233. }
  234.  
  235. inline nsStyleUnit nsStyleSides::GetBottomUnit(void) const
  236. {
  237.   return GetUnit(NS_SIDE_BOTTOM);
  238. }
  239.  
  240. inline nsStyleCoord& nsStyleSides::Get(PRUint8 aSide, nsStyleCoord& aCoord) const
  241. {
  242.   aCoord.SetUnionValue(mValues[aSide], (nsStyleUnit)mUnits[aSide]);
  243.   return aCoord;
  244. }
  245.  
  246. inline nsStyleCoord& nsStyleSides::GetLeft(nsStyleCoord& aCoord) const
  247. {
  248.   return Get(NS_SIDE_LEFT, aCoord);
  249. }
  250.  
  251. inline nsStyleCoord& nsStyleSides::GetTop(nsStyleCoord& aCoord) const
  252. {
  253.   return Get(NS_SIDE_TOP, aCoord);
  254. }
  255.  
  256. inline nsStyleCoord& nsStyleSides::GetRight(nsStyleCoord& aCoord) const
  257. {
  258.   return Get(NS_SIDE_RIGHT, aCoord);
  259. }
  260.  
  261. inline nsStyleCoord& nsStyleSides::GetBottom(nsStyleCoord& aCoord) const
  262. {
  263.   return Get(NS_SIDE_BOTTOM, aCoord);
  264. }
  265.  
  266. inline void nsStyleSides::Set(PRUint8 aSide, const nsStyleCoord& aCoord)
  267. {
  268.   mUnits[aSide] = aCoord.GetUnit();
  269.   aCoord.GetUnionValue(mValues[aSide]);
  270. }
  271.  
  272. inline void nsStyleSides::SetLeft(const nsStyleCoord& aCoord)
  273. {
  274.   Set(NS_SIDE_LEFT, aCoord);
  275. }
  276.  
  277. inline void nsStyleSides::SetTop(const nsStyleCoord& aCoord)
  278. {
  279.   Set(NS_SIDE_TOP, aCoord);
  280. }
  281.  
  282. inline void nsStyleSides::SetRight(const nsStyleCoord& aCoord)
  283. {
  284.   Set(NS_SIDE_RIGHT, aCoord);
  285. }
  286.  
  287. inline void nsStyleSides::SetBottom(const nsStyleCoord& aCoord)
  288. {
  289.   Set(NS_SIDE_BOTTOM, aCoord);
  290. }
  291.  
  292. #endif /* nsStyleCoord_h___ */
  293.